<?php
namespace Tools\Controller;
use Common\Controller\AdminbaseController;

class AdminAdvertController extends AdminbaseController{

	protected $ad_model;

	//广告分类
	//protected $class_model;

	public function _initialize() {
		parent::_initialize();
		$this->ad_model = D("ToolsAdvert");
		$this->class_model = D("ToolsAdvertClass");
	}

	// 后台广告列表
	public function index(){
		$ads=$this->ad_model->order("time desc")->select();
		foreach($ads as $v){
			$classid = $v['class_id'];
			$class = M('tools_advert_class')->field('info')->where("id = '$classid'")->find();
			$v['class_id'] = $class['info'];
			$a[] = $v;
		}
		$this->assign("ads",$a);
		$this->display();
	}

	
	// 后台广告列表
	public function classlist(){
		$class= M('tools_advert_class')->where("del = '0'")->order("time desc")->select();
		$this->assign("class",$class);
		$this->display();
	}

	// 广告分类删除
	public function classdelete(){
		$id = I("get.id",0,"intval");
		$data['del'] = 1;
		$del = M('tools_advert_class')->where("id = '$id'")->save($data);
		if ($del) {
			$this->success("删除成功！");
		} else {
			$this->error("删除失败！");
		}
	}
	//分类编辑
	public function editclass(){
		$id=I("get.id",0,'intval');
		$class=M('tools_advert_class')->where("id = '$id'")->find();

		$this->assign("class",$class);


		$this->display();
	}
	// 分类编辑提交
	public function editclass_post(){
		if(IS_POST){
			$id = I('id');
			$data = array();
			$data['info'] = I('advertC');
			$data['time'] = time();
			$save = M('tools_advert_class')->where("id = '$id'")->save($data);
			if ($save) {
			$this->success("保存成功！", U("AdminAdvert/classlist"));
			} else {
				$this->error("保存失败！");
			}
		}
		//dump($arr);
		// exit;
	}
	// 广告添加
	public function addone(){
		$aaa=$this->class_model->where("del = '0'")->select();
		$this->assign("tac",$aaa);
		$this->display();
	}

	// 广告添加提交
	public function add_post(){
		if(IS_POST){
			if ($this->ad_model->create()!==false){
				$classid = $_POST['class_id'];
				$name = $_POST['advert_name'];
				$url = $_POST['cs_picture'];
				$addr = $_POST['advert_addr'];
				$time = time();
				$arr = array("class_id"=>$classid,"name"=>"".$name."","pic"=>"".$url."","addr"=>"".$addr."","time"=>"".$time."");
				if ($this->ad_model->add($arr)!=false) {
					$this->success('添加成功！', U("AdminAdvert/index"));
				} else {
				    $this->error($this->ad_model->getError('1'));
					// $this->error('添加失败！');
				}
			} else {
				$this->error($this->ad_model->getError('错误'));
			}
		}
		//dump($arr);
		// exit;
	}

	// 广告编辑
	public function edit(){
		$id=I("get.id",0,'intval');
		$advert=$this->ad_model->where(array('id'=>$id))->find();
		$aaa=$this->class_model->select();
		$this->assign("tac",$aaa);

		$this->assign("advert",$advert);


		$this->display();
	}

	// 广告编辑提交
	public function edit_post(){
		if ($_POST) {

			if ($this->ad_model->create()!==false) {
				$id = $_POST['advert_id'];
				$classid = $_POST['class_id'];
				$name = $_POST['advert_name'];
				$url = $_POST['cs_picture'];
				$addr = $_POST['advert_addr'];
				$time = time();
				$arr = array("id"=>$id,"class_id"=>$classid,"name"=>$name,"pic"=>$url,"addr"=>$addr,"time"=>$time);
				if ($this->ad_model->save($arr)!==false) {
					$this->success("保存成功！", U("AdminAdvert/index"));
				} else {
					$this->error($this->ad_model->getError('错误'));
				}
			} else {
				$this->error($this->ad_model->getError('错误'));
			}
		}

	}

	//广告分类
	public function addclass(){


		$this->display();
	}

	// 广告分类提交
	public function addclass_post(){
		if(IS_POST){
			if ($this->class_model->create()!==false){
				$info = $_POST['advertC'];

				$time = time();
				$arr = array("class_id"=>"NULL","info"=>"".$info."","time"=>"".$time."");
				if ($this->class_model->add($arr)!=false) {
					$this->success('添加成功！', U("AdminAdvert/classlist"));
				} else {
				    $this->error($this->class_model->getError('1'));
					// $this->error('添加失败！');
				}
			} else {
				$this->error($this->class_model->getError('错误'));
			}
		}
		//dump($arr);
		// exit;
	}

	// 广告删除
	public function delete(){
		$id = I("get.id",0,"intval");
		if ($this->ad_model->delete($id)!==false) {
			$this->success("删除成功！");
		} else {
			$this->error("删除失败！");
		}
	}

	// 广告显示/隐藏
	public function toggle(){
		if(!empty($_POST['ids']) && isset($_GET["display"])){
			$ids = I('post.ids/a');
			if ($this->ad_model->where(array('ad_id'=>array('in',$ids)))->save(array('status'=>1))!==false) {
				$this->success("显示成功！");
			} else {
				$this->error("显示失败！");
			}
		}

		if(isset($_POST['ids']) && isset($_GET["hide"])){
			$ids = I('post.ids/a');
			if ($this->ad_model->where(array('ad_id'=>array('in',$ids)))->save(array('status'=>0))!==false) {
				$this->success("隐藏成功！");
			} else {
				$this->error("隐藏失败！");
			}
		}
	}

}